<?php


define('S_START',0);
define('S_COM_HASH',1);
define('S_BC_SLASH',2);
define('S_COM_STAR',3);
define('S_IC_STAR',4);
define('S_SQUOTE',5);
define('S_Q_BSLASH',6);
define('S_BUFF',7);
define('S_ENDBUFF',8);
define('S_COM_DASH',9);
define('S_BC_DASH',10);

class SetupSql
{

var $sqlfile;
var $server = 'localhost';
var $login = 'root';
var $pass = '';
//var $db;
var $port = '3306';
var $conn;

var $ar_commands;
var $commands_index = 0;
var $cmdbuffer;
var $ar_rawchars;

function SetVars($sqlfile, $login, $pass, $server){

	// login to server
	$this->server = $server;
	$this->login = $login;
	$this->pass = $pass;
	$this->sqlfile = $sqlfile;


}


function DoSetupSQL($sqlfile = 'setup.sql', $login , $pass = '', $server = 'localhost', $db=''){
	$this->SetVars($sqlfile, $login, $pass, $server);
	$this->Login();
	if(!empty($db)){
		$this->SetDB($db);
	}
	$this->ImportFile();
	//$this->PrintCommands();
	$this->RunCommands();

}


function Login(){
	$this->conn = mysql_connect($this->server, $this->login, $this->pass) or die(mysql_error());
	//mysql_select_db($dbDatabase,$conn)  or die(mysql_error());
}

function SetDB($db){
	mysql_select_db($db,$this->conn)  or die(mysql_error());
}

function ImportFile(){
	// open file into string
	$this->commands_index = 0;
	unset($this->ar_commands);
	$this->cmdbuffer = '';
	$this->ar_rawchars = '';
	
	$contentString = file_get_contents ($this->sqlfile);
	// cut string into array of characters
	$this->ar_rawchars = str_split($contentString);
	$state = S_START;
	foreach($this->ar_rawchars as $char) {
		$state = $this->ProcessChar($char, $state);
	}
}

function PrintCommands(){
	foreach($this->ar_commands AS $cmd){
		echo '<pre>' . $cmd . "</pre></br>\n";
	}
}

function RunCommands(){
	foreach($this->ar_commands AS $cmd){
		$result = mysql_query($cmd,$this->conn) or die(mysql_error());
	}
}

function ProcessChar($input, $state){
	if($state ==  S_START){
	switch ($input) {
	    case '#':
	        $nextstate = S_COM_HASH;
	        break;
	    case '-':
	        $nextstate = S_BC_DASH;
	        break;			
	    case '/':
	        $nextstate = S_BC_SLASH;
	        break;
	    case "'":
	        $nextstate = S_SQUOTE;
	        break;
	    case ';':
	        $nextstate = S_ENDBUFF;
	        break;
		default:
			$nextstate = S_BUFF;
			break;
		
		}	
	}
	else if($state ==  S_COM_HASH){
	}

	else if($state ==  S_BC_DASH){
		if($input == '-'){
			$nextstate = S_COM_DASH;
		}else{
			$nextstate = S_BUFF;
			$this->cmdbuffer .= $input;
			
		}
	
	}	
	
	else if($state ==  S_COM_DASH){
		if($input == chr(10)){
			$nextstate = S_BUFF;
		}else{
			$nextstate = S_COM_DASH;
		}
	}	
	
	else if($state == S_BC_SLASH){
		if($input == '*') {
			$nextstate = S_COM_STAR;
		}
		else{
			$nextstate = S_BUFF;
		}
	}
	
	else if($state == S_COM_STAR){
		if($input == '*'){
			$nextstate = S_IC_STAR;
		}
		else{
			$nextstate = S_COM_STAR;
		}
	
	}
	
	else if($state == S_IC_STAR){
		if($input == '/'){
			$nextstate = S_BUFF;
		}else{
			$nextstate = S_COM_STAR;
		}
	
	}
	
	else if($state == S_SQUOTE){
		
		switch ($input) {
		    case "'":
		        $nextstate = S_BUFF;
				$this->cmdbuffer .= $input;
		        break;
		    case '\\':
		        $nextstate = S_Q_BSLASH;
				$this->cmdbuffer .= $input;
		        break;
			default:
				$nextstate = S_SQUOTE;
				$this->cmdbuffer .= $input;
				break;
				}
	}
	
	else if($state == S_Q_BSLASH){
		$this->cmdbuffer .= $input;
		$nextstate = S_SQUOTE;
		
	}
	
	else if($state == S_BUFF){
		switch ($input) {
		    case '#':
		        $nextstate = S_COM_HASH;
		        break;
			case '-':
				$nextstate = S_BC_DASH;
				break;					
		    case '/':
		        $nextstate = S_BC_SLASH;
		        break;
		    case "'":
		        $nextstate = S_SQUOTE;
				$this->cmdbuffer .= $input;
		        break;
		    case ';':
		        $nextstate = S_BUFF;
				//$this->cmdbuffer .= $input;
				if(strlen(trim($this->cmdbuffer)) > 0){
					$this->ar_commands[$this->commands_index] = $this->cmdbuffer;
					$this->commands_index ++;
				}
				$this->cmdbuffer = '';
		        break;
			default:
				$nextstate = S_BUFF;
				$this->cmdbuffer .= $input;
				break;
		}	
	}
	
	else if($state == S_ENDBUFF){
	}


	return $nextstate;
	
}

}




?>